package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

import androidx.databinding.DataBindingUtil;


import com.amap.api.maps.MapView;
import com.swgk.core.util.LogUtil;
import com.sgb.alimaplibrary.MapMarkerTools;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMapGoodsBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.GoodsSourceLocationEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.transport.MapSourcesEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.widget.GoodsTypePopWindow3;
import com.sgb.kjwl.view.widget.MapGoodsPopWindow;
import com.sgb.kjwl.view.widget.SendGoodsLocationPopWindow3;
import com.sgb.kjwl.viewmodel.transport.MapGoodsModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


public class MapGoodsActivity extends AppActivity {
    private ActivityMapGoodsBinding binding;

    private Double currentLat;
    private Double currentLong;
    private MapGoodsPopWindow mapGoodsPopWindow;
    private SendGoodsLocationPopWindow3 sendGoodsLocationPopWindow3;
    private GoodsTypePopWindow3 goodsTypePopWindow;
    private List<GoodsInfoTypeEntity3> goodsTypeData;
    private int position = -1;
    private String radius = "10";
    private String provinceNo = "";
    private String cityNo = "";
    private String districtNo = "";

    // 一级联动数据
    public static List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    public static List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    public static List<List<List<AddressEntity>>> opt3 = new ArrayList<>();


    @Inject
    MapGoodsModel viewModel;
    private MapMarkerTools mapMarkerTools;
    private MapView mMapView;
    List<MapSourcesEntity> bundles = new ArrayList<>();

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MapGoodsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapMarkerTools = new MapMarkerTools(getActivity(),getLifecycle());
        initMap(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(MapGoodsActivity.this, R.layout.activity_map_goods);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.include.rightView.setVisibility(View.GONE);
        binding.include.titleView.setText("地图找货");
        binding.startLayout.setOnClickListener(this);
        binding.endLayout.setOnClickListener(this);
        binding.typeLayout.setOnClickListener(this);
        mapGoodsPopWindow = new MapGoodsPopWindow(MapGoodsActivity.this);

        //发货省份popWindow
        sendGoodsLocationPopWindow3 = new SendGoodsLocationPopWindow3(getActivity());
        sendGoodsLocationPopWindow3.setBgView(binding.grayLayout);
        sendGoodsLocationPopWindow3.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object proId, Object cityId, Object areaId) {
                GoodsSourceLocationEntity entity = (GoodsSourceLocationEntity) proId;
                binding.tvSend.setText(entity.getCityName());
                provinceNo = entity.getProvinceNo();
                cityNo = entity.getCityNo();
                districtNo = entity.getAreaNo();
                viewModel.getLL(provinceNo,cityNo,districtNo);
            }
        });
//        sendGoodsLocationPopWindow3.setOnDismissListener(() -> binding.sendGoodsIv.setSelected(false));

        goodsTypePopWindow = new GoodsTypePopWindow3(getActivity());
        goodsTypePopWindow.setBgView(binding.grayLayout);
        goodsTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                GoodsInfoTypeEntity3 entity = (GoodsInfoTypeEntity3)type;
                binding.tvRadius.setText(entity.getName());
                radius = entity.getNo();
                position = (int) key2;
                LogUtil.v("goodsInfo","货物类型="+entity.getName());
                viewModel.getLL(provinceNo,cityNo,districtNo);
            }
        });
//        goodsTypePopWindow.setOnDismissListener(() -> binding.goodsTypeIv.setSelected(false));

    }

    @Override
    protected void init() {
        viewModel.getAddressModel();
        //经纬度
        viewModel.getLL(provinceNo,cityNo,districtNo);
        viewModel.getRadiusList();
    }

    private void initMap(Bundle savedInstanceState) {
        mMapView = binding.mapView;
        if (mMapView.getChildCount() >= 2){
            View child = mMapView.getChildAt(1);
            if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
                child.setVisibility(View.INVISIBLE);
            }
        }
        mapMarkerTools.init(savedInstanceState, mMapView);
        mapMarkerTools.setMarkerClick(new MapMarkerTools.MarkerClick() {
            @Override
            public void onMarkerClick(int pos,String title) {
                showMarkerView(pos,title);
            }

            @Override
            public void onLocationChanged(Double currentLat, Double currentLong, String address) {

            }

        });
    }

    public void showMarkerView(int pos,String title) {
        MapSourcesEntity entity = bundles.get(pos);

        mapGoodsPopWindow.showPopwindowBottom(binding.mapView);
        mapGoodsPopWindow.setData(entity);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_layout:
                if(null!=addressEntityList && addressEntityList.size()>0){
                    showSendPopWindow();
                }else{
                    viewModel.getAddressModel();
                }
                break;
            case R.id.type_layout:
                if(null!=goodsTypeData && goodsTypeData.size()>0){
                    showGoodsTypePopWindow();
                }else{
                    viewModel.getRadiusList();
                }
                break;
        }
    }

    private void cameraToPosition() {
        if (currentLat == 0 || currentLong == 0) {
            return;
        }
        mapMarkerTools.setMyLocation(currentLat,currentLong);
    }

    public void getRadiusList(List<GoodsInfoTypeEntity3> list){
        goodsTypeData = list;
    }

    public void getLL(MapLongAndLatEntity list){
        if(null!=list){
            currentLat = Double.valueOf(list.getLat());
            currentLong = Double.valueOf(list.getLng());
            viewModel.getMapList(currentLong+"",currentLat+"",radius);
            cameraToPosition();
        }
    }

    private void addMarkInfoWindow(List<MapSourcesEntity> datas) {
        try {
            mapMarkerTools.mapClear();
            bundles.clear();
            for (int i = 0; i < datas.size(); i++) {
                if (null != datas && datas.size() > 0) {
                    MapSourcesEntity entity = datas.get(i);
                    if (null != entity && !TextUtils.isEmpty(entity.getLocation().get(1)) && !TextUtils.isEmpty(entity.getLocation().get(0))) {
                        double currentLat = Double.parseDouble(entity.getLocation().get(1));
                        double currentLong = Double.parseDouble(entity.getLocation().get(0));
                        if (currentLat == 0 || currentLong == 0) {
                            return;
                        }
                        mapMarkerTools.addPoiItem(currentLat, currentLong,"");
                        bundles.add(datas.get(i));
                    }
                }
            }
            mapMarkerTools.addAllMarkerToMap(R.mipmap.ic_map_overlay,false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showMapList(List<MapSourcesEntity> datas) {
        LogUtil.v("goodsInfo",datas.size()+"==mark大小");
        addMarkInfoWindow(datas);
    }

    public void initAddressData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        addressEntityList = datas;
        initJsonData();
    }

    private void initJsonData() {

        for (int i = 0; i < addressEntityList.size(); i++) {

//            cityList = new ArrayList<>();
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {

                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
    }

    private void showSendPopWindow(){
        sendGoodsLocationPopWindow3.showPopwindow(binding.idRootLayout,0,0);
        sendGoodsLocationPopWindow3.setData(addressEntityList);
    }

    private void showGoodsTypePopWindow(){
        goodsTypePopWindow.showPopwindow(binding.idRootLayout,0,0);
        goodsTypePopWindow.setData(goodsTypeData,position);
    }

    //    public void showCityPopWindow(List<DictEntity3> data) {
//        if (data != null && data.size() > 0) {
//            currentLat = Double.valueOf(data.get(0).getLatitude());
//            currentLong = Double.valueOf(data.get(0).getLongitude());
//            conditionMap.put("cityNo", data.get(0).getId());
//            viewModel.getSupplierMapModel(conditionMap);
//            cameraToPosition();
//        }
//    }

//    public void showMap(List<SupplierEntity3> datas) {
//        mapSupplierList = datas;
//        List<OverlayOptions> overlayOptions = addMarkInfoWindow(mapSupplierList);
//        baiduMap.addOverlays(overlayOptions);
//    }

     /*private List<OverlayOptions> addMarkInfoWindow(List<SupplierEntity3> supplierEntity3List) {
        List<OverlayOptions> overlayOptions = new ArrayList<>();
        for (SupplierEntity3 supplierEntity3 : supplierEntity3List) {
            LatLng latLng = new LatLng(Double.valueOf(supplierEntity3.getLatitude()), Double.valueOf(supplierEntity3.getLongitude()));
            MapInfoWindow3 mapInfoWindow = new MapInfoWindow3(this);
            mapInfoWindow.setData(supplierEntity3);
            InfoWindow infoWindow = new InfoWindow(mapInfoWindow, latLng, -200);
            BitmapDescriptor bd1 = BitmapDescriptorFactory.fromResource(R.mipmap.map_goods_mark);
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng)
                    .icon(bd1).infoWindow(infoWindow).draggable(true);
            overlayOptions.add(markerOptions);
        }
        return overlayOptions;
    }*/
}
